﻿using System;
using System.Collections.Generic;
using System.IO;
using ZQNB.Common.Encrypt;
using ZQNB.Common.Serialize;

namespace ZQNB.Common.License
{
    public class MyPublicLicenseHelper
    {
        public static MyLicenseShell<T> ReadFile<T>(string path)
        {
            MyLicenseShell<T> myLicense = null;
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("参数不能为空");
            }
            if (!File.Exists(path))
            {
                throw new ArgumentException("文件不存在");
            }

            try
            {
                //从物理文件中导出MyLicense的实例
                myLicense = MySerializeXmlHelper.DeSerializeFile<MyLicenseShell<T>>(path);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("文件格式损坏" + ex.Message);
            }
            return myLicense;
        }

        public static bool TryVerify<T>(string path, string xmlPublicKey)
        {
            bool result = false;
            try
            {
                MyLicenseShell<T> info = ReadFile<T>(path);
                //验证是否是正确的厂商的签名
                bool isTheMarkOk = TryVerifyMark(info.TheMarkSignature, xmlPublicKey);
                if (isTheMarkOk)
                {
                    string licenseValue = MySerializeXmlHelper.SerializeToXmlText(info.LicenseValue);
                    result = TryVerifyValue(licenseValue, xmlPublicKey, info.LicenseSignature);
                }
            }
            catch (Exception ex)
            {
                string exMessage = ex.Message;
            }
            return result;
        }

        private static bool TryVerifyMark(string theMarkSignature, string xmlPublicKey)
        {
            bool isTheMarkOk = false;
            try
            {
                //验证是否是正确的厂商的签名
                isTheMarkOk = MySignatureHelper.VerifyByPublicKey(Properties.Resources.SignatureMark, xmlPublicKey, theMarkSignature);
            }
            catch (Exception ex)
            {
                string exMessage = ex.Message;
            }
            return isTheMarkOk;
        }

        private static bool TryVerifyValue(string licenseValue, string xmlPublicKey, string licenseSignature)
        {
            bool result = false;
            try
            {
                result = MySignatureHelper.VerifyByPublicKey(licenseValue, xmlPublicKey, licenseSignature);
            }
            catch (Exception ex)
            {
                string exMessage = ex.Message;
            }
            return result;
        }
        
        //用指定的公钥列表，来确定出是哪个厂商的签名
        public static string TryFindOutWhichXmlPublicKey<T>(string path, List<string> xmlPublicKeyList)
        {
            string xmlPublicKey = null;
            if (xmlPublicKeyList != null && xmlPublicKeyList.Count > 0)
            {
                //验证是否是正确的厂商的签名
                MyLicenseShell<T> info = ReadFile<T>(path);
                foreach (string publicKey in xmlPublicKeyList)
                {
                    bool isTheMarkOk = TryVerifyMark(info.TheMarkSignature, publicKey);
                    if (isTheMarkOk)
                    {
                        xmlPublicKey = publicKey;
                        break;
                    }
                }
            }
            return xmlPublicKey;
        }
    }
}
